<!--
 * @Descripttion: 编辑轮播图
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-19 17:45:39
-->
<template>
  <div class="lb-app-swiper-add">
    <top-nav :title="navTitle" :isBack="true"></top-nav>
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="swiperForm"
        :rules="swiperFormRules"
        ref="swiperForm"
        label-width="140px"
      >
        <el-form-item label="轮播图" prop="img">
          <lb-cover
            :fileList="swiperForm.img"
            @selectedFiles="getSwiper"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸：750 * 400</lb-tool-tips>
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            v-model="swiperForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitFormInfo('swiperForm')">{{
            $t('action.submit')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      swiperForm: {
        img: [],
        top: 0
      },
      swiperFormRules: {
        img: { required: true, type: 'array', message: '请上传图片', trigger: 'change' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      }
    }
  },
  created () {
    let { id } = this.$route.query
    this.navTitle = this.$t(id ? 'menu.EditSwiper' : 'menu.AddSwiper')
    if (id) {
      this.swiperForm.id = id
      this.getFormInfo(id)
    }
  },
  methods: {
    getFormInfo (id) {
      this.$api.getSweepPaymentBannerInfo({ id }).then(res => {
        if (res.code === 200) {
          this.swiperForm.img = [{ url: res.data.img }]
          this.swiperForm.top = res.data.top
        }
      })
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let subForm = JSON.parse(JSON.stringify(this.swiperForm))
          subForm.img = subForm.img.map(item => { return item.url })
          this.$api.addEditSweepPaymentBanner(subForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
              this.goBack()
            }
          })
        }
      })
    },
    getSwiper (img) {
      this.swiperForm.img = img
    },
    goBack () {
      this.$router.back(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-app-swiper-add {
  width: 100%;
  .page-main {
    .el-input,
    .el-input-number {
      width: 300px;
    }
  }
}
</style>
